import createPersistedState from 'vuex-persistedstate';

// 存储整个模块
const zyjInfo = createPersistedState({
  key: 'zyj',
  storage: window.sessionStorage,//window.localStorage
  paths: ['id', 'imageUrl', 'account', 'name', 'sex', 'identity', 'department', 'email', 'readList']
})


// import { userInfo, changeReadList } from "@/api/user";
// import {
//   getReadList,
// } from "@/api/message";

import { createStore } from 'vuex'

export default createStore({
  state: {
    id: "",
    imageUrl: "",
    account: "",
    name: "",
    sex: "",
    identity: "",
    department: "",
    email: "",
    readList: []
  },
  getters: {
  },
  mutations: {
    setReadList(state, info) {
      state.readList = info;
    },
    setInfo(state, info) {
      state.id = info.id;
      state.imageUrl = "http://127.0.0.1:7111" + info.imageUrl;
      state.account = info.account;
      state.name = info.name;
      state.sex = info.sex;
      state.identity = info.identity;
      state.department = info.department;
      state.email = info.email;
    },
    setName(state, name) {
      state.name = name;
    },
    setSex(state, sex) {
      state.sex = sex;
    },
    setEmail(state, email) {
      state.email = email;
    },
    setImageUrl(state, imageUrl) {
      state.imageUrl = "http://127.0.0.1:7111" + imageUrl;
    }
  },
  actions: {
    async getUserInfo({ commit }) {
      const res = await userInfo();
      if (res.data.data.read_status == 0) {
        let res1 = await changeReadList({ department: res.data.data.department });
        commit('setReadList', res1.data.data) // 将接口数据写入项目状态中
      } else {
        let res1 = await getReadList();
        commit('setReadList', res1.data.data) // 将接口数据写入项目状态中
      }
      commit('setInfo', res.data.data) // 将接口数据写入项目状态中
    }
  },
  modules: {
  },
  // 将各个插件引用
  plugins: [zyjInfo]
})
